﻿Chenxin.UI.ContractInfoAdd=Chenxin.extend(Chenxin.UI.PageCommon,{
	actionUrl:'/service/logic/ContractMgr.ashx',
	createForm:function(){
		return new Chenxin.UI.ContractInfoForm({
			actionUrl:this.actionUrl,
			afterGetFormJson:function(model){
				Chenxin.applyIf(model,this.currentModel);
			},
			beforeFitData:function(model){
				if(model.CityId){					
					var company=this.form.findByName('CompanyId');
					company.param.fatherid=model.CityId;
					company.reload(model.CompanyId);
					
					this.form.findByName('Contract_PropertyCode')[model.Contract_PropertyCode?'enable':'disable']();
					this.form.findByName('Contract_OwnerCode')[model.Contract_OwnerCode?'enable':'disable']();
					this.form.findByName('Contract_LandUseCode')[model.Contract_LandUseCode?'enable':'disable']();
					this.form.findByName('Contract_OtherCode')[model.Contract_OtherCode?'enable':'disable']();
					
					$('.x_contract_use')[model.Contract_Type==10?'hide':'show']();
					this.form.findByName('Contract_LateFree')[model.Contract_Type==10?'disable':'enable']();
				}
			},
			scope:this
		});
	},
	initData:function(){
		contractId=Chenxin.Fn.getQueryString('contract_id');
		if(contractId){
			Chenxin.Fn.getModel(contractId,this.actionUrl,function(model){
				this.fitData(model);
				this.op='edit';
				this.scope.currentModel=model;
				document.title='编辑合同';
			},this.form)
		}else{
			this.form.op='add';
		}
	},
	initComponent:function(){
		Chenxin.UI.ContractInfoAdd.superclass.initComponent.call(this);
		this.initData();
	}
});

Chenxin.UI.ContractInfoForm=Chenxin.extend(Chenxin.UI.FormCommon,{
	type:'numal',
	id:'x_contract_info',
	initEvent:function(){
		var _this=this;
		$('#'+this.id+' em').click(function(){			
			$(this.parentNode.parentNode).next()[this.innerHTML=='收起'?'slideUp':'slideDown']('fast');
			$(this)[this.innerHTML=='收起'?'addClass':'removeClass']('x_up');
			this.innerHTML=(this.innerHTML=='收起'?'展开':'收起');
		});
		
		this.findByName('Contract_Type').bind('click',function(){
			$('.x_contract_use')[this.id=='x_contract_type1'?'hide':'show']();
			
			var _clf=_this.findByName('Contract_LateFree');
			_clf.val(this.id=='x_contract_type1'?'1':'');
			if(this.id=='x_contract_type1'){
				_clf.disable();//attr('disabled','disabled');
			}else{
				_clf.enable();//removeAttr('disabled');
			}
		});
		this.findByName('Contract_PayMethod').bind('click',function(){
			$('.x_contract_payuserinfo')[this.id=='x_contract_paymethod1'?'hide':'show']();
		});
		
		$('.x_contract_housecode').bind('click',function(){
			var _propertyName='';
			switch(this.id){
				case 'x_contract_housecode1':
					_propertyName='Contract_PropertyCode';
					break;
				case 'x_contract_housecode2':
					_propertyName='Contract_OwnerCode';
					break;
				case 'x_contract_housecode3':
					_propertyName='Contract_LandUseCode';
					break;
				case 'x_contract_housecode4':
					_propertyName='Contract_OtherCode';
					break;
			}
			if(this.checked){
				_this.findByName(_propertyName).enable();//removeAttr('disabled');	
			}else{	
				_this.findByName(_propertyName).disable();//attr('disabled','disabled');	
			}
		});
		
        $("#markedTime").focus(function() {
            WdatePicker({ oncleared: function() { $(this).blur(); }, onpicked: function() { $(this).blur(); } })
        })
		$('.x_contract_date').focus(function() {
			$(this).attr('readonly','readonly');
            WdatePicker({ oncleared: function() { $(this).blur(); }, onpicked: function() { $(this).blur(); } });
        });
		$('.x_contract_number').numberText();
		
		$('#contract_save').click(function(){
			_this.save();
			return false;
		})
		$('#contract_reset').click(function(){
			_this.reset();
			return false;
		});
		
		$('.x-quick-free').quickInput({
			colNum:4,
			colWidth:100,
			items:['水费','电费','煤气费','通讯费','有线电视费','网络费','物业管理费','居间费','相关税费']
		});
		$('.x-quick-payupper').quickInput({
			colNum:14,
			addSplit:false,
			colWidth:20,
			items:['壹','贰','叁','肆','伍','陆','柒','捌','玖','拾','佰','仟','万','亿']
		})
	},
	createComboList:function(){
		
		//城市
		var city=new Chenxin.UI.StructCombo({
			param:{op:'getcity'},
			containerId:'#Contract_CityId',
			name:'CityId',
			inModelType:'all',
			txtModelName:'CityName',
			tbxWidth:151,
			colWidth:73,
			afterLoadItems:function(){
				if(this.items.length==1){
					this.addPleaseSelected=false;
					this.defVal=this.items[0].val;
					this.defTxt=this.items[0].txt;
					company.param.fatherid=this.defVal;
					company.reload();
				}else{
					this.addPleaseSelected=true;
					this.defVal='请选择';
					this.defTxt='请选择';				
				}
			},
			onSelected:function(){
				if(this.getValue()==this.defVal){
					company.clearItems();
				}else{					
					company.param.fatherid=this.getValue();
					company.reload();
				}
			}
		});
		
		//公司
		var company=new Chenxin.UI.StructCombo({
			autoLoad:false,
			param:{op:'getcompany'},
			name:'CompanyId',
			inModelType:'all',
			txtModelName:'CompanyName',
			containerId:'#Contract_CompanyId',
			tbxWidth:151,
			colWidth:73,
			afterLoadItems:function(){
				if(this.items.length==1){
					this.addPleaseSelected=false;
					this.defVal=this.items[0].val;
					this.defTxt=this.items[0].txt;
				}else{
					this.addPleaseSelected=true;
					this.defVal='请选择';
					this.defTxt='请选择';							
				}
			}
		});
		return [city,company]
	},
	saveSucc:function(data,msg){
		if(this.form.op=='add'){
			var content='';
			content+='<a id="'+this.id+'_print">预览并打印</a>';
			content+='<br/><br/><a href="/page/ContractInfo/ContractMgr.aspx">合同列表</a>';
			content+='<br/><br/><a href="/page/ContractInfo/ContractInfo.aspx?contract_id='+data.contract_id+'">编辑该合同</a>';
			Chenxin.Msg.alert.show(content,'保存成功,下一步：',Chenxin.Msg.icon.next,function(){
				window.location.href='/page/ContractInfo/ContractMgr.aspx';
			});
		}else{
			window.location.href='/page/ContractInfo/ContractMgr.aspx';
		}
	},
	beforeFitData:function(model){
		var company=Chenxin.UI.ContractInfoForm.superclass.findByName('companyid');
		company.param.cityid=model.CityId;
		company.reload(model.CompanyId);
	},
	initComponent:function(){
		Chenxin.UI.ContractInfoForm.superclass.initComponent.call(this);
	}
});

$(document).ready(function(){
	var fc=new Chenxin.UI.ContractInfoAdd();
});